package IC.Tests;

import java.io.IOException;

import IC.CompilerImpl;
import IC.RunParameters;
import IC.Utils;
import junit.framework.TestCase;

public class LoweringTest extends TestCase 
{
	private static final String _libPath = "src\\IC\\Tests\\TestCode\\libic.sig";
	private static final String _testLib = "src\\IC\\Tests\\TestCode\\";

	public void testRegisters()
	{
		testFile("Registers01");
	}
	
	public void testRegisters2()
	{
		testFile("Registers02");
	}
	
	public void testRegisters3()
	{
		testFile("Registers03");
	}
	
	public void testTemp()
	{
		testFile("ParamsSending");
	}
	
	public void testStringCat()
	{
		testFile("StringCat");
	}
	
	public void testExpressionEvaluationInCompileTime()
	{
		testFile("ExpressionEvaluationInCompileTime");
	}
	
	public void testBubbleSort()
	{
		testFile("BubbleSort");
	}
	
	public void testFib()
	{
		testFile("fib");
	}
	
	public void testBreakContinue()
	{
		testFile("BreakContinue");
	}
	
	public void testVirtualFunctionCall()
	{
		testFile("VirtualFunctionCall");
	}
	
	public void testStaticFunctionCall()
	{
		testFile("StaticFunctionCall");
	}
	
	public void testScope()
	{
		testFile("Scope");
	}
	
	public void testWhile()
	{
		testFile("While");
	}
	
	public void testWhile2()
	{
		testFile("WhileTest2");
	}
	
	public void testMathOperators()
	{
		testFile("MathOperators");
	}
	
	public void testIf()
	{
		testFile("If");
	}
	
	public void testPrint()
	{
		testFile("Print");
	}
	
	
	public void testLogicalOperators()
	{
		testFile("LogicalOperators");
	}
	
	public void testStringPlus()
	{
		testFile("StringPlus");
	}
	
	public void testPrinti()
	{
		testFile("PrintI");
	}
	
	public void testInitialization()
	{
		testFile("TestInitialization");
	}
	
	public void testReturn()
	{
		testFile("ReturnTest");
	}
	
	public void testArrayLocation()
	{
		testFile("TestArrayLocation");
	}
	
	private void testFile(String fileNameWithoutExtention)
	{
		String[] compilerParams = { _testLib + fileNameWithoutExtention + ".ic",  "-L" + _libPath, "-print-lir", "-opt-lir"};
		assertTrue("Compliation process stoped", 
				new CompilerImpl(new RunParameters(compilerParams)).Compile());
		
		String[] simulatorParams = { _testLib + fileNameWithoutExtention + ".lir"};
		microLIR.test.TestOutput.clear();
		
		System.out.println("Executing the Code : \n\n");
		microLIR.Main.main(simulatorParams);
		
		String LIROutput = microLIR.test.TestOutput.read().replaceAll("\\r", "");
		String expectedOuput = GetFileContain(fileNameWithoutExtention).replaceAll("\\r", "");
		
		assertNotNull(
				String.format(
						"Can't read the expected output from %s%s.test",
						_testLib,
						fileNameWithoutExtention),
				expectedOuput);
		
		assertEquals(expectedOuput, LIROutput);
	}
	
	private String GetFileContain(String fileName)
	{
		java.io.Reader reader = Utils.GetReader(_testLib + fileName + ".result");
    	if (reader == null)
    	{
    		System.out.println("Couldn't open file: " + fileName);
    		return null;
    	}
    	
    	String ret = "";
    	try
    	{
    		int readChar;
    		while ((readChar = reader.read()) != -1)
    			ret += (char)readChar;
    	} catch (IOException e){
    		return null;
    	}
    	
    	return ret;
	}
	
}
